<?php

namespace app\erp\model;

use think\Model;
use traits\model\SoftDelete;

class Uploaddb extends Model
{
    use SoftDelete;

    protected $deleteTime = 'delete_time';
    protected $pk = 'upload_id';
    protected $name = 'upload';
    protected $type = [
        'create_time' => 'timestamp:Y-m-d',
        'delete_time' => 'timestamp:Y-m-d'
    ];
    protected $autoWriteTimestamp = true;
    protected $insert = [
        'partner_id' => PARTNER_ID,
        'md5'
    ];
    public $param = array();

    // 自动完成md5
    protected function setMd5Attr()
    {
        return md5(PARTNER_ID . time());
    }

    // 基础查询
    public function base($query)
    {
        $field = $this->getDeleteTimeField(true);
        if ($field) {
            if (isset ($this->param ['delete']) && $this->param ['delete'] == 'yes') {
                $this->onlyTrashed();
            } else {
                $query->useSoftDelete($field);
            }
        }
        $query->where('partner_id', PARTNER_ID);
    }

    // 通过上传数据获取信息
    public function ajaxFind()
    {
        $this->where('md5', $this->setMd5Attr());
        $info = $this->master()->find();
        if ($info) {
            $data = $info->toArray();
            $data = $this->handleData($data);
            return $data;
        } else {
            return false;
        }
    }

    public function ajaxFindOne($upload_id)
    {
        $this->where('upload_id', $upload_id);
        $info = $this->master()->find();
        if ($info) {
            $data = $info->toArray();
            $data = $this->handleData($data);
            return $data;
        } else {
            return false;
        }
    }

    public function getFindTemp()
    {
        $data ['save_path'] = $_SERVER ['REQUEST_SCHEME'] . '://' . $_SERVER ['HTTP_HOST'] . '/static/img/no_img.jpg';
        $data ['save_path_thumb'] = $_SERVER ['REQUEST_SCHEME'] . '://' . $_SERVER ['HTTP_HOST'] . '/static/img/no_img.jpg';
        $data ['upload_id'] = 0;
        return $data;
    }

    public function getSelectTemp()
    {
        $data [0] ['save_path'] = $_SERVER ['REQUEST_SCHEME'] . '://' . $_SERVER ['HTTP_HOST'] . '/static/img/no_img.jpg';
        $data [0] ['save_path_thumb'] = $_SERVER ['REQUEST_SCHEME'] . '://' . $_SERVER ['HTTP_HOST'] . '/static/img/no_img.jpg';
        $data [0] ['upload_id'] = 0;
        return $data;
    }

    // 通过传入编号获取信息
    public function getFind($upload_id)
    {
        if (!is_numeric($upload_id)) {
            $data ['save_path'] = $upload_id;
            $data ['save_path_thumb'] = $upload_id;
            $data ['upload_id'] = $upload_id;
            $data ['file_name'] = '上传文件';
            return $data;
        } else {
            $this->where('upload_id', $upload_id);
            $info = $this->find();
            if ($info) {
                $data = $info->toArray();
                $data = $this->handleData($data);
                return $data;
            } else {
                $data ['save_path'] = $_SERVER ['REQUEST_SCHEME'] . '://' . $_SERVER ['HTTP_HOST'] . '/static/img/no_img.jpg';
                $data ['save_path_thumb'] = $_SERVER ['REQUEST_SCHEME'] . '://' . $_SERVER ['HTTP_HOST'] . '/static/img/no_img.jpg';
                $data ['upload_id'] = '';
                return $data;
            }
        }
    }

    public function getSelect($upload_ids)
    {
        if ($upload_ids) {
            for ($i = 0; $i < count($upload_ids); $i++) {
                if (!is_numeric($upload_ids [$i])) {
                    $data [$i] ['save_path'] = $upload_ids [$i];
                    $data [$i] ['save_path_thumb'] = $upload_ids [$i];
                    $data [$i] ['upload_id'] = $upload_ids [$i];
                } else {
                    $data_temp = $this->where('upload_id', $upload_ids [$i])->find();
                    if ($data_temp) {
                        $data_temp = $data_temp->toArray();
                        $data_temp = $this->handleData($data_temp);
                        $data [$i] = $data_temp;
                    } else {
                        $data [$i] = array();
                    }
                }
            }
            return $data;
        } else {
            return false;
        }
    }

    public function ajaxSave()
    {
        $this->save($this->param);
        return $this->ajaxFindOne($this->data ['upload_id']);
    }

    public function ajaxAddOne()
    {
        $this->save($this->param);
        return $this->data ['upload_id'];
    }

    // 软删除
    public function ajaxDelete()
    {
        if (isset ($this->param ['upload_id'])) {
            if (isset ($this->param ['delete']) && $this->param ['delete'] == 'yes') {
                $this->destroy(function ($query) {
                    $query->where('upload_id', 'IN', $this->param ['upload_id']);
                }, true);
            } else {
                $this->destroy(function ($query) {
                    $query->where('upload_id', 'IN', $this->param ['upload_id']);
                }, true);
            }
        }
        return 'success';
    }

    // 数据处理
    protected function handleData($val)
    {
        /*$siteConfigdb = new SiteConfigdb ();
        $siteConfigdb->param ['config_name'] = 'ISTHUMB';
        $get_isthumb = $siteConfigdb->getValue();*/
        $get_isthumb = '否';
        foreach ($val as $k => $v) {
            $val [$k] = $v;
            switch ($k) {
                case 'save_path' :
                    $temp = explode ( ',', $val ['save_path'] );
                    if(count($temp) >1) {
                        $val['save_path_all'] = $temp;
                        $val['save_path'] = $temp[0];
                    } else {
                        if ($val ['is_outlink'] == 1) {
                            /*
                            $temp = explode ( '.', $val ['save_path'] );
                            $path = $temp [0] . '_thumb.' . $temp [1];
                            $val ['file_path'] = $path;
                            if ($get_isthumb == '是') {
                                $val ['save_path_thumb'] = $_SERVER ['REQUEST_SCHEME'] . '://' . $_SERVER ['HTTP_HOST'] . '/uploads/' . $path;
                            }
                            */
                            if(!strstr($val['save_path'],'http')){
                                $val ['save_path'] = $_SERVER ['REQUEST_SCHEME'] . '://' . $_SERVER ['HTTP_HOST'] . '/uploads/' . $val ['save_path'];
                            }
                            if(SITE_ID == 117) {
                                $val['save_path_all'][] = $val ['save_path'];
                            }
                            if ($get_isthumb == '是') {
                                $val ['save_path_thumb'] = $val ['save_path'];
                            }
                        } else {
                            $val ['save_path'] = $v;
                            $val ['save_path_thumb'] = $v;
                        }

                    }

                    break;
                default :
                    break;
            }
        }
        return $val;
    }
}